package io.lb.deng.webflux.web.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * @Project
 * @Module
 * @JDK_Vserion 11
 * @Author DengLibin
 * @Email dlb@smarthse.cn
 * @Desc 接口统一返回对象
 * @since 2020/3/12 0012 15:02
 */
@Data
@ApiModel("接口统一返回对象")
public class ResponseData<T extends Serializable> implements Serializable {

    private static final long serialVersionUID = 6115763584010270155L;

    @ApiModelProperty(value = "结果数据", required = false)
    private T data; // 结果数据
    // 写一个空参数的构造函数，用于反序列化
    @ApiModelProperty(value = "结果码", required = true, example = "1")
    private Integer code; // 结果码
    @ApiModelProperty(value = "提示信息", required = false)
    private String msg; // 提示信息

    public ResponseData() {

    }

    private ResponseData(StatusCodeEnum codeEnum, T data) {

        this.data = data;
        this.code = codeEnum.getCode();
        this.msg = codeEnum.getMsg();
    }

    private ResponseData(int code, String msg, T data) {
        this.data = data;
        this.code = code;
        this.msg = msg;
    }

    /**
     * 成功
     * 
     * @param data
     * @param <W>
     * @return
     */
    public static <W extends Serializable> ResponseData<W> success(W data) {
        return new ResponseData<>(StatusCodeEnum.SUCCESS, data);
    }

    /**
     * 成功(自定义，前端不会拦截，由具体业务决定如何处理)
     * 
     * @param message : 业务提示消息
     * @param data    : 可以放自定义业务状态标志位
     * @return
     */
    public static <W extends Serializable> ResponseData<W> success(String message, W data) {
        return new ResponseData<>(StatusCodeEnum.SUCCESS.getCode(), message, data);
    }

    /**
     * 失败
     * 
     * @param message : 失败后的提示数据
     * @return
     */
    public static <T extends Serializable> ResponseData<T> fail(String message) {
        return new ResponseData<>(StatusCodeEnum.FAIL.getCode(), message, null);
    }

    /**
     * 失败
     * 
     * @param data : 失败操作后返回的业务数据
     * @return
     */
    public static <T extends Serializable> ResponseData<T> fail(T data) {
        return new ResponseData<>(StatusCodeEnum.FAIL.getCode(), null, data);
    }

    /**
     * 服务端异常
     * 
     * @param message
     * @return
     */
    public static <T extends Serializable> ResponseData<T> error(String message) {
        return new ResponseData<>(StatusCodeEnum.ERROR.getCode(), message, null);
    }

    /**
     * 请求错误
     * 
     * @param
     * @return
     */
    public static ResponseData<String> requestError() {
        return new ResponseData<>(StatusCodeEnum.REQUEST_ERROR, null);
    }

    /**
     * 请求错误
     * 
     * @param message 提示信息
     * @return
     */
    public static <T extends Serializable> ResponseData<T> requestError(String message) {
        return new ResponseData<>(StatusCodeEnum.REQUEST_ERROR.getCode(), message, null);
    }

    /**
     * 禁止访问错误
     * 
     * @return
     */
    public static ResponseData<String> forbiddenError() {
        return new ResponseData<>(StatusCodeEnum.FORBIDDEN_ERROR, null);
    }
    
    /**
     * 未认证异常
     * 
     * @return
     */
    public static ResponseData<String> noLoginError() {
        return new ResponseData<>(StatusCodeEnum.NO_LOGIN, null);
    }


}
